package com.mt.fpb.controller;

import com.mt.fpb.enums.ResponseCode;
import com.mt.fpb.model.vo.CommonResult;
import com.mt.fpb.utils.KeyUtil;
import com.mt.fpb.utils.RedisUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.*;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;


@RestController
@RequestMapping("/SmsController")
public class SmsController {

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("sendMassage")
    public CommonResult sendMassage(String phone){
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKIDPaT5Q1nS5RWKr6s2bttsb4HDsGNDhldD ", "Su5zbw4yf4k9H7hgQs33qMbcNq3ykLgk");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String str = "86"+phone;
            String[] phoneNumberSet1 = {str};
            req.setPhoneNumberSet(phoneNumberSet1);


            req.setSmsSdkAppId("1400575660");
            req.setSignName("睿享保险");
            //req.setTemplateId("1135328");
            req.setTemplateId("1152362");

//            Random r = new Random();
//            Integer s = r.nextInt(9999);
            String code = KeyUtil.keyUtils();
           // String code = (Math.random()*9+1)*1000+ "";

            String[] templateParamSet1 = {code};
            req.setTemplateParamSet(templateParamSet1);

            //HttpSession session = n
/*            session.setAttribute("MsCode", String.valueOf(templateParamSet1));
            session.setMaxInactiveInterval(5);*/

            // 存储到redis
            redisUtil.set(phone, code, 300);
            System.out.println("立即获取存放在redis中的code");
            String strCode = (String) redisUtil.get(phone);
            System.out.println("立即获取的code:"+strCode);
            //String msCode = (String) redisUtil.get(phone);


            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
            return CommonResult.success(ResponseCode.SUCCESS);
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
            return CommonResult.success(ResponseCode.FAILED);
        }


    }

//    static String secretId = "1400575660";
//    static String secretKey = "94493f0cfab3a5e4e3ba6d27eadb43b7";

   /* public static void main(String [] args) {
        try{
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential("AKIDPaT5Q1nS5RWKr6s2bttsb4HDsGNDhldD ", "Su5zbw4yf4k9H7hgQs33qMbcNq3ykLgk");
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"+8613479605198"};
            req.setPhoneNumberSet(phoneNumberSet1);


            req.setSmsSdkAppId("1400575660");
            req.setSignName("睿享保险");
            req.setTemplateId("1135328");

            Random r = new Random();
            Integer s = r.nextInt(9999);

            System.out.println("s:"+s.toString());
            String[] templateParamSet1 = {s.toString()};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }*/
    }





